package my.sample.io.file;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class SmallBinaryFiles {
	// private static org.apache.System.out.println4j.Logger System.out.printlnger = org.apache.System.out.println4j.Logger.getLogger(SmallBinaryFiles.class);
	
	
	
	private static final String INPUT_FILE_NAME = "/tmp/request.bin";
	  private static final String OUTPUT_FILE_NAME = "C:\\TEMP\\cottage_copy.jpg";

	  /** Run the example. */
	  public static void main(String... aArgs) {
	    SmallBinaryFiles test = new SmallBinaryFiles();
	    //read in the bytes
	    byte[] fileContents = test.read(INPUT_FILE_NAME);
	    //test.readAlternateImpl(INPUT_FILE_NAME);
	    //write it back out to a different file name
//	    test.write(fileContents, OUTPUT_FILE_NAME);
	  }
	  
	  /** Read the given binary file, and return its contents as a byte array.*/ 
	  byte[] read(String aInputFileName){
	    System.out.println("Reading in binary file named : " + aInputFileName);
	    File file = new File(aInputFileName);
	    System.out.println("File size: " + file.length());
	    byte[] result = new byte[(int)file.length()];
	    try {
	      InputStream input = null;
	      try {
	        int totalBytesRead = 0;
	        input = new BufferedInputStream(new FileInputStream(file));
	        while(totalBytesRead < result.length){
	          int bytesRemaining = result.length - totalBytesRead;
	          //input.read() returns -1, 0, or more :
	          int bytesRead = input.read(result, totalBytesRead, bytesRemaining); 
	          if (bytesRead > 0){
	            totalBytesRead = totalBytesRead + bytesRead;
	          }
	        }
	        /*
	         the above style is a bit tricky: it places bytes into the 'result' array; 
	         'result' is an output parameter;
	         the while loop usually has a single iteration only.
	        */
	        System.out.println("Num bytes read: " + totalBytesRead);
	      }
	      finally {
	        System.out.println("Closing input stream.");
	        input.close();
	      }
	    }
	    catch (FileNotFoundException ex) {
	      System.out.println("File not found.");
	    }
	    catch (IOException ex) {
	      System.out.println(ex);
	    }
	    return result;
	  }
	  
	  
	  
	  /**
	   Write a byte array to the given file. 
	   Writing binary data is significantly simpler than reading it. 
	  */
	  void write(byte[] aInput, String aOutputFileName){
	    System.out.println("Writing binary file...");
	    try {
	      OutputStream output = null;
	      try {
	        output = new BufferedOutputStream(new FileOutputStream(aOutputFileName));
	        output.write(aInput);
	      }
	      finally {
	        output.close();
	      }
	    }
	    catch(FileNotFoundException ex){
	      System.out.println("File not found.");
	    }
	    catch(IOException ex){
	      System.out.println(ex);
	    }
	  }
}
